package com.zaoniao.enstudy.api.controller;

import com.zaoniao.enstudy.api.dto.AnswerDto;
import com.zaoniao.enstudy.domain.entity.ExamPaper;
import com.zaoniao.enstudy.domain.entity.bo.ExamPaperBo;
import com.zaoniao.enstudy.domain.service.ExamService;
import com.zaoniao.enstudy.infracore.web.BaseResponse;
import com.zaoniao.enstudy.infracore.web.DataResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 学习测试相关api.
 */
@Tag(name = "学习测试相关api")
@RestController
@RequestMapping("/exam")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ExamController {

    private final ExamService examService;

    @Operation(summary = "1.测试题目生成", description = "根据单词表生成测试题明细(谨慎使用，会清理单词表)")
    @GetMapping("/words-pager")
    public BaseResponse wordsPager() {
        examService.allWordsPager();
        return DataResponse.ok("测试题目生成成功！");
    }

    @Operation(summary = "2.用户生成考题", description = "z_exam_log 以及 z_exam_log_items 表数据的生成")
    @PutMapping("/generate/{examCount}")
    public BaseResponse generate(@Parameter(name = "examCount", description = "生成题目数目")
                                 @PathVariable Integer examCount,
                                 @RequestParam String userId) {
        ExamPaperBo examPaperBo = examService.examGenerate(examCount, userId);
        // 返回生成的测试记录以及关联题目列表
        return DataResponse.ok(examPaperBo);
    }

    @Operation(summary = "3.开始考试", description = "用户点击开始考试，更新 z_exam_paper 表的开始时间，以及状态")
    @PutMapping("/start/{paperId}")
    public BaseResponse answerStart(@Parameter(name = "paperId", description = "考试记录Id")
                                    @PathVariable Integer paperId) {
        examService.examStart(paperId);
        return DataResponse.ok("开始考试!");
    }

    @Operation(summary = "4.用户答题", description = "答题完毕，更新 z_exam_answer 的用户提交的答案")
    @PostMapping("/answer-submit")
    public BaseResponse answerSubmit(@RequestBody @Valid AnswerDto answerDto) {
        examService.answerSubmit(AnswerDto.Converter.INSTANCE.dto2do(answerDto));
        return DataResponse.ok("提交答案成功");
    }

    @Operation(summary = "5.结束考试", description = "用户点击结束考试，更新 z_exam_paper 表的结束时间，以及状态")
    @PutMapping("/end/{paperId}")
    public BaseResponse answerEnd(@Parameter(name = "paperId", description = "考试记录Id")
                                  @PathVariable Integer paperId) {
        examService.examEnd(paperId);
        return DataResponse.ok("考试结束");
    }

    @Operation(summary = "6.考试记录", description = "用户查询自己的考试记录")
    @GetMapping("/pager-list/{userId}")
    public BaseResponse examList(@Parameter(name = "userId", description = "用户Id,即:OpenId")
                                 @PathVariable String userId) {
        List<ExamPaper> paperList = examService.examList(userId);
        return DataResponse.ok(paperList);
    }

}
